package com.zmkm.service.impl;

import com.zmkm.common.ServerResponse;
import com.zmkm.dao.HousekeeperMapper;
import com.zmkm.entity.Housekeeper;
import com.zmkm.service.IUserService;
import com.zmkm.util.StringOrInt;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("iUserService")
public class UserServiceImpl implements IUserService {
	@Autowired
	private HousekeeperMapper housekeeperMapper;

	@Override
	public ServerResponse userNameAndpassWordVerify(String userName, String passWord) {
		Housekeeper housekeeper;
		if (userName.length() == 11 && StringOrInt.stringVerify(userName)) {
			housekeeper = housekeeperMapper.selectByPhone(userName);
			if (housekeeper != null) {
				if (housekeeper.getPassWord().equals(passWord))
					return ServerResponse.createBySuccess(housekeeper);
				else
					return ServerResponse.createByErrorMessage("密码错误！");
			} else {
				return ServerResponse.createByErrorMessage("没有该手机账号!");
			}
		}
		housekeeper = housekeeperMapper.selectByUserName(userName);
		if (housekeeper != null) {
			if (housekeeper.getPassWord().equals(passWord))
				return ServerResponse.createBySuccess(housekeeper);
			else
				return ServerResponse.createByErrorMessage("密码错误！");
		} else {
			return ServerResponse.createByErrorMessage("没有该账号!");
		}
	}

	@Override
	public ServerResponse userInsert(Housekeeper housekeeper) {
		if (housekeeperMapper.selectByPhone(housekeeper.getPhone()) != null) {
			return ServerResponse.createByErrorMessage("添加账号失败,手机已被注册！");
		}
		if (housekeeperMapper.selectByUserName(housekeeper.getUserName()) != null) {
			return ServerResponse.createByErrorMessage("添加账号失败,用户名已被注册！");
		}
		Housekeeper identity_card = new Housekeeper();
		identity_card.setIdentity_card(housekeeper.getIdentity_card());;
		List<Housekeeper> list = housekeeperMapper.selectHousekeeper(identity_card);
		if (list!=null && !list.isEmpty()) {
			return ServerResponse.createByErrorMessage("不能一个人注册两个管家号哦！");
		}
		if (housekeeperMapper.insertSelective(housekeeper) <= 0)
			return ServerResponse.createByErrorMessage("添加账号失败");
		else
			return ServerResponse.createBySuccessMessage("添加账号成功");
	}

	@Override
	public ServerResponse selectByOneUser(String switchNumber, String search) {
		Housekeeper housekeeper = new Housekeeper();
		String str;
		switch ((str = switchNumber).hashCode()) {
		case -866751801:
			if (str.equals("housekeeper_address")) {
				housekeeper.setHousekeeper_address(search);
			}
			break;
		case -748022413:
			if (str.equals("identity_address")) {
				housekeeper.setIdentity_address(search);
			}
			break;
		case 3373707:
			if (str.equals("name")) {
				housekeeper.setName(search);
			}
			break;
		case 106642798:
			if (str.equals("phone")) {
				housekeeper.setPhone(search);
			}
			break;
		case 562006513:
			if (str.equals("identity_card")) {
				housekeeper.setIdentity_card(search);
			}
			break;
		case 1923664483:
			if (str.equals("user_grade"))
				housekeeper.setUser_grade(Integer.valueOf(Integer.parseInt(search)));
			break;
		}
		List list = housekeeperMapper.selectHousekeeper(housekeeper);
		if (!list.isEmpty())
			return ServerResponse.createBySuccess(list);
		else
			return ServerResponse.createByErrorMessage("没有这个管家的信息喔！");
	}

	@Override
	public Housekeeper selectById(Integer id) {
		Housekeeper housekeeper = this.housekeeperMapper.selectByPrimaryKey(id);
		return housekeeper;
	}

	@Override
	public ServerResponse updateById(Housekeeper housekeeper) {
		int i = housekeeperMapper.updateByPrimaryKeySelective(housekeeper);
		if (i <= 0)
			return ServerResponse.createByErrorMessage("管家信息修改失败");
		else
			return ServerResponse.createBySuccessMessage("管家信息修改成功");
	}

	@Override
	public ServerResponse delectById(Integer id) {
		int i = housekeeperMapper.deleteByPrimaryKey(id);
		if (i <= 0)
			return ServerResponse.createByErrorMessage("管家信息删除失败");
		else
			return ServerResponse.createBySuccessMessage("管家信息删除成功");
	}
}
